<?php
/**
 * +--------------------------------------------------------+
 * @Created by Phpstorm.
 * @Category StockCapService.php
 * @Depiction
 * @Author hwy <429208948@qq.com>
 * @Date 2022/7/4  13:55
 * +--------------------------------------------------------+
 */

namespace App\Services\Swoole\Components;


use App\Jobs\DataCapJob;
use App\Services\MyStockService;
use App\Services\StockService;
use App\Util\Container;
use Carbon\Carbon;
use Illuminate\Support\Facades\Cache;

class StockCapService implements ComponentsInterface
{

    public function handle()
    {
        $now = Carbon::now();
        if ($now->isWeekday() == false) {
            return false;
        }

        $y  = $now->year;//
        $m  = $now->month;//
        $d  = $now->day;//
        $h  = $now->hour;//
        $mi = $now->minute;//
        if ($h == 9 && $mi == 20) {  //9    10
            //数据存入数据库 清空缓存
            // Container::run(StockFundsService::class, 'addDatas');
            Cache::flush();
        }

        if ($h < 12) {
            $first  = Carbon::create($y, $m, $d, 9, 25);
            $second = Carbon::create($y, $m, $d, 11, 31);
            if (!$now->between($first, $second)) {
                return false;
            }
        } else {
            $first  = Carbon::create($y, $m, $d, 13);//13
            $second = Carbon::create($y, $m, $d, 15, 1);//15
            if (!$now->between($first, $second)) {
                return false;
            }
        }

        $t          = time();
        $stock_list = Container::run(MyStockService::class, 'getStocks', [1]);
        foreach ($stock_list as $key => $val) {
            $params['time'] = $t;
            $params['key']  = $key;
            dispatch((new DataCapJob($params))->onQueue('default'));
        }

        return $res = response_success_msg('请求成功！');

    }


}
